<template>
  <div class="register-page">
    <nav-bar></nav-bar>
    <div class="register-content">
      <div class="register-container">
        <div class="register-left">
          <img src="@/assets/register-illustration.png" alt="注册插图" class="large-illustration">
        </div>
        <div class="register-right">
          <h2>欢迎加入学了喵</h2>
          <div class="custom-input-container">
            <input 
              v-model="username"
              class="custom-input" 
              placeholder="请输入用户名" 
              maxlength="20"
            />
          </div>
          <div class="custom-input-container">
            <input 
              v-model="password"
              class="custom-input" 
              type="password"
              placeholder="请输入密码" 
              maxlength="20"
            />
          </div>
          <div class="custom-input-container">
            <input 
              v-model="confirmPassword"
              class="custom-input" 
              type="password"
              placeholder="请确认密码" 
              maxlength="20"
            />
          </div>
          
          <button class="register-btn" @click="handleRegister">注册</button>
          
          <div class="register-desc">
            已有账号可
            <router-link to="/login" class="login-link">直接登录</router-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import NavBar from '@/components/NavBar.vue'
import axios from 'axios'

export default {
  name: 'RegisterView',
  components: {
    NavBar
  },
  data() {
    return {
      username: '',
      password: '',
      confirmPassword: ''
    }
  },
  methods: {
    async handleRegister() {
      if (!this.username || !this.password || !this.confirmPassword) {
        alert('请填写完整信息');
        return;
      }
      if (this.password !== this.confirmPassword) {
        alert('两次输入的密码不一致');
        return;
      }
      try {
        const response = await axios.post('/api/auth/register', {
          username: this.username,
          password: this.password,
          confirmPassword: this.confirmPassword
        });
        if (response.data.code === 200) {
          alert('注册成功，请登录');
          this.$router.push('/login');
        } else {
          alert(response.data.message || '注册失败');
        }
      } catch (error) {
        alert(error.response?.data?.message || '注册失败');
      }
    }
  }
}
</script>

<style scoped>
.register-page {
  width: 100%;
  min-height: 100vh;
  background-color: #fff;
}

.register-content {
  padding-top: 80px;
}

.register-container {
  width: 1000px;
  margin: 0 auto;
  display: flex;
  gap: 60px;
  align-items: center;
}

.register-left {
  flex: 1;
}

.register-left img {
  width: 100%;
  max-width: 500px;
}

.large-illustration {
  width: 120%;
  max-width: 600px;
  margin-left: -40px;
}

.verify-code-container {
  display: flex;
  gap: 10px;
}

.verify-code-input {
  flex: 1;
}

.verify-code-btn {
  width: 120px;
  height: 45px;
  background: white;
  border: 1px solid #6d28d2;
  color: #6d28d2;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.verify-code-btn:hover {
  background: rgba(109, 40, 210, 0.05);
}

.register-right {
  flex: 1;
  padding: 40px;
  max-width: 400px;
}

h2 {
  font-size: 32px;
  color: #333;
  margin-bottom: 25px;
  font-weight: 500;
}

.custom-input-container {
  margin: 20px 0;
  width: 100%;
}

.custom-input {
  width: 100%;
  height: 45px;
  border: 1px solid #333;
  border-radius: 0;
  padding: 0 15px;
  font-size: 14px;
  box-sizing: border-box;
  outline: none;
}

.custom-input:focus {
  border-color: #6d28d2;
}

.register-desc {
  color: #666;
  font-size: 14px;
  margin-top: 25px;
}

.register-btn {
  width: 100%;
  height: 45px;
  background: #6d28d2;
  color: white;
  border: none;
  font-size: 16px;
  cursor: pointer;
  margin-top: 30px;
}

.other-login {
  margin: 30px 0;
}

.divider {
  text-align: center;
  position: relative;
  color: #999;
  font-size: 14px;
  margin: 20px 0;
}

.divider::before,
.divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 30%;
  height: 1px;
  background-color: #ddd;
}

.divider::before {
  left: 0;
}

.divider::after {
  right: 0;
}

.social-login {
  display: flex;
  justify-content: center;
  gap: 30px;
  margin-top: 20px;
}

.social-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid #ddd;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s;
}

.social-btn:hover {
  border-color: #6d28d2;
  transform: translateY(-2px);
}

.social-btn img {
  width: 20px;
  height: 20px;
}

.login-link {
  color: #6d28d2;
  text-decoration: none;
  cursor: pointer;
}

.login-link:hover {
  text-decoration: underline;
}
</style>